package com.sdry.basic.service;


import com.sdry.basic.entity.*;
import com.sdry.basic.mapper.TimeAttendanceMapper;
import com.sdry.untils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 *@ClassName: TimeAttendanceServiceImpl
 *@Description: 考勤
 *@Author tdd
 *@Date 2020-2-21 15:38:03
 *@version 1.0
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class TimeAttendanceServiceImpl implements TimeAttendanceService {
	@Resource
	TimeAttendanceMapper timeAttendanceMapper;
	/**
	 * 条件查询对应考勤组
	 * @param param 实体条件
	 * @return 实体集合
	 */
	public List<TimeAttendanceGroup> queryAllByMutionTimeAttendanceGroup(TimeAttendanceGroup param){
		return timeAttendanceMapper.queryAllByMutionTimeAttendanceGroup(param);
	}
	/**
	 * 条件分页查询考勤记录
	 * @param param 实体条件
	 * @return 实体集合
	 */
	public List<TimeAttendance> findPageByMutionTimeAttendance(TimeAttendance param){
		return timeAttendanceMapper.findPageByMutionTimeAttendance(param);
	}
	/**
	 * 条件查询考勤记录条数
	 * @param param 实体条件
	 * @return 实体集合
	 */
	public Integer selectCountByMutionTimeAttendance(TimeAttendance param){
		return timeAttendanceMapper.selectCountByMutionTimeAttendance(param);
	}
	/**
	 * 根据主键更新考勤组 返回影响行数
	 * @param param 实体条件
	 * @return 影响行数
	 */
	public Integer updateTimeAttendanceGroup(TimeAttendanceGroup param){
		Integer num =  timeAttendanceMapper.updateTimeAttendanceGroup(param);
		if (num > 0){
			List<TimeAttendanceStare> list = param.getWeeks();
			for(int i=0 ;i > list.size();i++){
				TimeAttendanceStare timeAttendanceStare = list.get(i);
				timeAttendanceMapper.updateTimeAttendanceStare(timeAttendanceStare);
			}
		}
		return num;
	}
	/**
	 * 添加考勤记录
	 * @param param 实体条件
	 * @return 影响行数
	 */
	public Long addtimeAttendanceRecord(TimeAttendanceRecord param){
		TimeAttendanceGroup timeAttendanceGroup = new TimeAttendanceGroup();
		List<TimeAttendanceGroup> list = timeAttendanceMapper.queryAllByMutionTimeAttendanceGroup(timeAttendanceGroup);
		Long id = timeAttendanceMapper.addtimeAttendanceRecord(param);
		if (null != id && list.size() > 0){
			timeAttendanceGroup = list.get(0);
			List<TimeAttendanceStare> list1 = timeAttendanceGroup.getWeeks();
			for(int i=0 ;i > list1.size();i++){
				String week = DateUtil.getWeekOfDate(new Date());
				TimeAttendanceStare timeAttendanceStare = list1.get(i);
				if(week.equals(timeAttendanceStare.getTasWeek())){
					TimeAttendance timeAttendance = new TimeAttendance();
					timeAttendance.setTadate(DateUtil.dateFormat1());
					timeAttendance.setTauid(param.getTaruid());
					List<TimeAttendance> TimeAttendances = timeAttendanceMapper.findPageByMutionTimeAttendance(timeAttendance);
					if(TimeAttendances.size() > 0 && TimeAttendances.get(TimeAttendances.size()-1).getGoTime() != null){
						timeAttendance = TimeAttendances.get(TimeAttendances.size()-1);
					}
					TimeAttendanceRule timeAttendanceRule = timeAttendanceGroup.getTimeAttendanceRule();
					if(null == timeAttendance.getGoTime() || "".equals(timeAttendance.getGoTime())){
						timeAttendance.setGotarid(id);
						timeAttendance.setGoTime(param.getTartime());
						SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						String time1 = timeAttendanceRule.getTaruGoTime();
						try {
							Date date1 = format.parse(time1);
							String time2 = timeAttendance.getGoTime();
							Date date2 = format.parse(time2);
							// 获得两个时间的毫秒时间差异
							long diff = date1.getTime()+Integer.valueOf(timeAttendanceRule.getTaruPermitLateTime())*60*1000 - date2.getTime();
							if(diff >= 0){
								timeAttendance.setTaresult("正常");
							}else{
								timeAttendance.setTaresult("迟到");
							}
						} catch (ParseException e) {
							e.printStackTrace();
						}
					}else{
						timeAttendance.setLeavetarid(id);
						timeAttendance.setLeaveTime(param.getTartime());
						SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						String time1 = timeAttendanceRule.getTaruLeaveTime();
						try {
							Date date1 = format.parse(time1);
							String time2 = timeAttendance.getLeaveTime();
							Date date2 = format.parse(time2);
							// 获得两个时间的毫秒时间差异
							long diff = date2.getTime() - (date1.getTime()-Integer.valueOf(timeAttendanceRule.getTaruPermitEarlyTime())*60*1000);
							if(diff >= 0){
								timeAttendance.setTaresult("早退");
							}
						} catch (ParseException e) {
							e.printStackTrace();
						}
					}
					//休息
					if("0".equals(timeAttendanceStare.getTasStare())){
						timeAttendance.setTaresult("加班");
					}else if("1".equals(timeAttendanceStare.getTasStare())){

					}
					timeAttendanceMapper.updateTimeAttendance(timeAttendance);
					break;
				}
			}
		}
		return id;
	}
}
